package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    // 在数据库创建一张表 user id username password email  注册[两次密码要一致] -- 添加数据 登录 -- 按照条件查询数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        String query = req.getQueryString();
        // key -- 请求参数的key（username） value -- > =右边的值
        Map<String, String[]> parameterMap = req.getParameterMap();
        String username = "root";
        String password = "kfm";
        if (query != null) {
            // 按照 & 分割 请求参数
            String[] params = query.split("&");
            Map<String , String> map = new HashMap<>();
            for (String param : params) {
                String[] split = param.split("=");
                map.put(split[0], split[1]);
            }
            // 遍历了所有的请求参数
            map.forEach((k, v) -> System.out.println(k + "==" + v));
            // 判断是否登录成功
            // map.get("username").equals(username);
            // select * from user where username = ? and password = ?
            if (username.equals(map.get("username")) && password.equals(map.get("password"))) {
                System.out.println("登录成功");
            } else {
                System.out.println("登录失败");
            }
        } else {
            System.out.println("请输入有效的参数");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    public static void main(String[] args) {
    }
}
